package mq

import (
	"ac-julink/cache"
	"ac-julink/tool/config"
	"fmt"

	//"github.com/jjeffery/stomp"
	"ac-julink/manager"
	"github.com/go-stomp/stomp"
)

type mqclient interface {
	SendMessage(string) error
	InitClientPool()
}

type MqListenerService interface {
	OnReceive(*stomp.Message, *cache.SyncContext, *manager.AcRestClient) error
}

var MyMqClient mqclient

func InitMQClient() {
	MyMqClient = &rabbitClient{}
	MyMqClient.InitClientPool()
}

func SendMessage(messageInfo string) error {
	return MyMqClient.SendMessage(messageInfo)
}

func StartWgdataMqListener(listenerService MqListenerService, context *cache.SyncContext, client *manager.AcRestClient, username, password string, mqSSL int) {
	mqListener := &activeMqListener{listenerService}
	brokerUrl := config.GetConfigValue("WGNS", "wgconfig_server_broker_uri")
	destination := config.GetConfigValue("WGNS", "wgconfig_download_queue_name")
	clientId := fmt.Sprintf("%s%s", "WGNS_Config", config.GetConfigValue("WGNS", "sid"))
	go mqListener.Start(brokerUrl, destination, clientId, context, client, username, password, mqSSL)
}

func StartWgCommonMqListener(listenerService MqListenerService, context *cache.SyncContext, client *manager.AcRestClient, username, password string, mqSSL int) {
	mqListener := &activeMqListener{listenerService}
	brokerUrl := config.GetConfigValue("WGNS", "wgcommon_server_broker_uri")
	destination := config.GetConfigValue("WGNS", "wgcommon_download_queue_name")
	clientId := fmt.Sprintf("%s%s", "WGNS_Common", config.GetConfigValue("WGNS", "sid"))
	go mqListener.Start(brokerUrl, destination, clientId, context, client, username, password, mqSSL)
}

func StartWgctlMqListener(listenerService MqListenerService, context *cache.SyncContext, client *manager.AcRestClient, username, password string, mqSSL int) {
	mqListener := &activeMqListener{listenerService}
	brokerUrl := config.GetConfigValue("WGNS", "ctl_server_broker_uri")
	destination := config.GetConfigValue("WGNS", "ctl_queue_name")
	clientId := fmt.Sprintf("%s%s", "WGNS_CTL", config.GetConfigValue("WGNS", "sid"))
	go mqListener.Start(brokerUrl, destination, clientId, context, client, username, password, mqSSL)
}
func StartWgotaMqListener(listenerService MqListenerService, context *cache.SyncContext, client *manager.AcRestClient, username, password string, mqSSL int) {
	mqListener := &activeMqListener{listenerService}
	brokerUrl := config.GetConfigValue("WGNS", "ota_server_broker_uri")
	destination := config.GetConfigValue("WGNS", "ota_queue_name")
	clientId := fmt.Sprintf("%s%s", "WGNS_OTA", config.GetConfigValue("WGNS", "sid"))
	go mqListener.Start(brokerUrl, destination, clientId, context, client, username, password, mqSSL)
}
func StartWgssMqListener(listenerService MqListenerService, context *cache.SyncContext, client *manager.AcRestClient, username, password string, mqSSL int) {
	mqListener := &activeMqListener{listenerService}
	brokerUrl := config.GetConfigValue("WGNS", "ss_server_broker_uri")
	destination := config.GetConfigValue("WGNS", "ss_queue_name")
	clientId := fmt.Sprintf("%s%s", "WGNS_SS", config.GetConfigValue("WGNS", "sid"))
	go mqListener.Start(brokerUrl, destination, clientId, context, client, username, password, mqSSL)
}
func StartWgmcuMqListener(listenerService MqListenerService, context *cache.SyncContext, client *manager.AcRestClient, username, password string, mqSSL int) {
	mqListener := &activeMqListener{listenerService}
	brokerUrl := config.GetConfigValue("WGNS", "mcu_server_broker_uri")
	destination := config.GetConfigValue("WGNS", "mcu_queue_name")
	clientId := fmt.Sprintf("%s%s", "WGNS_MCU", config.GetConfigValue("WGNS", "sid"))
	go mqListener.Start(brokerUrl, destination, clientId, context, client, username, password, mqSSL)
}

func StartBlackListNCleanCacheListener(listenerService MqListenerService, context *cache.SyncContext, client *manager.AcRestClient, username, password string, mqSSL int) {
	mqListener := &activeMqListener{listenerService}
	brokerUrl := config.GetConfigValue("WGNS", "black_list_server_broker_uri")
	destination := config.GetConfigValue("WGNS", "black_list_topic")
	clientId := fmt.Sprintf("%s%s", "WGNS_BLACK_LIST", config.GetConfigValue("WGNS", "sid"))
	go mqListener.Start(brokerUrl, destination, clientId, context, client, username, password, mqSSL)
}
